package cn.itheima.dao.impl;
/*
* 持久层实现类
* */
import cn.itheima.dao.StudentDao;
import cn.itheima.domain.Student;

import java.sql.*;
import java.util.ArrayList;

public class StudentDaoImpl implements StudentDao {

    @Override
    public ArrayList<Student> findAll() {
        ArrayList<Student> list = new ArrayList<>();
        Connection con = null;
        Statement stat = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db14", "root", "root");
            stat = con.createStatement();
            String sql = "select * from student";
            rs = stat.executeQuery(sql);
            while(rs.next()) {
                Integer sid = rs.getInt("sid");
                String name = rs.getString("name");
                Integer age = rs.getInt("age");
                Date birthday = rs.getDate("birthday");
                Student stu = new Student(sid,name,age,birthday);
                list.add(stu);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(stat != null) {
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }
        return list;
    }

    @Override
    public Student findById(Integer id) {

        Student stu = null;
        Connection con = null;
        Statement stat = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db14", "root", "root");
            stat = con.createStatement();
            String sql = "select * from student where sid = '"+ id +" '";
            rs = stat.executeQuery(sql);
            while(rs.next()) {
                Integer sid = rs.getInt("sid");
                String name = rs.getString("name");
                Integer age = rs.getInt("age");
                Date birthday = rs.getDate("birthday");
                 stu = new Student(sid,name,age,birthday);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(stat != null) {
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }
        return stu;
    }

   /* @Override
    public int insert() {
     return null;
    }*/
}
